/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.liwalo.action;

import java.io.IOException;

import javax.mail.MessagingException;

import org.apache.commons.lang3.RandomStringUtils;

import com.liwalo.dao.UsuarioDAO;
import com.liwalo.entities.Usuario;
import com.liwalo.utils.MailSenderUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author Carlos
 */
public class Remember extends ActionSupport{
    
    private static String REMEMBER = "remember";
    private String email;
    

	public String newPass() throws Exception{
		MailSenderUtil sender = MailSenderUtil.getInstance();
		String password = RandomStringUtils.randomAlphanumeric(8);
		
		UsuarioDAO dao = new UsuarioDAO();
		Usuario user = dao.getUserByEmail(email);
		
		if(user == null){
			addActionError("Email no encontrado");
			return INPUT;
		}
		
		user.setContrasena(password);
		dao.update(user);
		
		try{
			sender.sendPassword(email, password);
		}catch(IOException e){
			return ERROR;
		}catch(MessagingException e){
			return ERROR;
		}
		
		
		return REMEMBER;
    }


	public String getEmail() {
		return email;
	}


	public void setEmail(String email) {
		this.email = email;
	}
	
	
}
